package ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.peng.auctionsystem.R;

import java.util.List;

import entity.CommondityListEntity;
import manager.ApiConstants;
import utils.Constants;

/**
 * Created by 浪 on 2018/4/7.
 */

/**
 * “我的”界面相关竞品适配器
 */
public class LotsDetailAdapter extends BaseAdapter {

    private List<CommondityListEntity.DataBean.RowsBean> mCommodityListData;
    private Context mContext;
    private ILotsCallback mCallback;
    private int flag;// 1-我的发布 2-流拍 3-已拍出 4-我参与的

    public LotsDetailAdapter(Context context, int flag, List<CommondityListEntity.DataBean.RowsBean> commodityListData) {
        this.mContext = context;
        this.flag = flag;
        mCommodityListData = commodityListData;
    }

    public void updateAdapter(List<CommondityListEntity.DataBean.RowsBean> list) {
        mCommodityListData.clear();
        mCommodityListData.addAll(list == null ? mCommodityListData : list);
        notifyDataSetChanged();
    }

    public void addCallback(ILotsCallback callback) {
        this.mCallback = callback;
    }

    @Override
    public int getCount() {
        return mCommodityListData.size();
    }

    @Override
    public Object getItem(int position) {
        return mCommodityListData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.layout_lot_detail_item, null);
            holder.mIvImg = convertView.findViewById(R.id.lot_detail_iv_img);
            holder.mTvTitle = convertView.findViewById(R.id.lot_detail_tv_title);
            holder.mTvDesc = convertView.findViewById(R.id.lot_detail_tv_desc);
            holder.mIvOver = convertView.findViewById(R.id.lot_detail_iv_over);
            holder.mLlFuntion = convertView.findViewById(R.id.lot_detail_ll_function);
            holder.mTvEdit = convertView.findViewById(R.id.lot_detail_tv_edit);
            holder.mTvDelete = convertView.findViewById(R.id.lot_detail_tv_delete);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.mTvTitle.setText(mCommodityListData.get(position).getTitle());
        holder.mTvDesc.setText(mCommodityListData.get(position).getRemark());
        Glide.with(mContext).load(ApiConstants.STAGING_MOBILE_TICKET_HOST + "commodity/" + mCommodityListData.get(position).getImagePath()).into(holder.mIvImg);

        //TODO 已结束标识现在是全部隐藏状态，到时候根据竞品数据来控制隐藏与现实
        if (flag == Constants.MY_LOT || flag == Constants.MY_JOIN_LOT) {
            holder.mIvOver.setVisibility(View.GONE);
        } else {
            holder.mIvOver.setVisibility(View.VISIBLE);
        }
        if (flag == Constants.MY_LOT) {
            holder.mLlFuntion.setVisibility(View.VISIBLE);
            holder.mTvEdit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mCallback != null) {
                        mCallback.onLotEdit(position);
                    }
                }
            });
            holder.mTvDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mCallback != null) {
                        mCallback.onLotDelete(position);
                    }
                }
            });
        } else {
            holder.mLlFuntion.setVisibility(View.GONE);
        }
        return convertView;
    }

    public class ViewHolder {
        ImageView mIvImg;// 竞品图片
        TextView mTvTitle;// 竞品标题
        TextView mTvDesc;// 竞品描述
        ImageView mIvOver;// 已结束标识（竞品流拍或已拍卖时显示）
        LinearLayout mLlFuntion;// 右侧功能列表（“我发布的”显示，其他隐藏）、
        TextView mTvEdit;// 编辑
        TextView mTvDelete;// 删除
    }

    public interface ILotsCallback {
        /**
         * 竞品编辑
         *
         * @param position
         */
        public void onLotEdit(int position);

        /**
         * 竞品删除
         *
         * @param position
         */
        public void onLotDelete(int position);
    }
}
